package com.shop.shoporder.wapController;

import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.StringUtil;
import com.shop.shoporder.entity.GoodsBanner;
import com.shop.shoporder.entity.HttpRequestConfig;
import com.shop.shoporder.entity.ShortUrl;
import com.shop.shoporder.service.GoodsBannerService;
import com.shop.shoporder.service.ShortUrlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@CrossOrigin
@Controller
@RequestMapping("/wap")
public class BannerController {

    @Autowired
    GoodsBannerService goodsBannerService;

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    ShortUrlService shortUrlService;

    Logger logger = LoggerFactory.getLogger(BannerController.class);

    /**
     * 获取首页banner
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getBanners")
    public Response getBanners(HttpServletRequest request) {
        StringBuffer sb = null;
        String bannerURL = null;
        int goodsId = 0;
        int specId = 0;
        List<GoodsBanner> goodsBannerList = new ArrayList<>();

        //读取缓存数据
        Object object = null;
        object = redisTemplate.opsForValue().get("banner");
        if (object != null) {
            goodsBannerList = (List<GoodsBanner>) object;
        } else {
            goodsBannerList = goodsBannerService.getBanners();
        }

        if (!goodsBannerList.isEmpty()) {
            for (int i = 0; i < goodsBannerList.size(); i++) {
                System.out.println("--->" + i);
                bannerURL = goodsBannerList.get(i).getBannerUrl();
//                goodsId = goodsBannerList.get(i).getGoodsId();
//                specId = goodsBannerList.get(i).getSpecId();
                sb = new StringBuffer();
                sb.append(bannerURL);
                if (goodsId != 0 || specId != 0) {
                    sb.append("?Id=" + goodsId);
                    sb.append("&specId=" + specId);
                }
                goodsBannerList.get(i).setBannerUrl(sb.toString());

            }
        }

        return new Response(MessageConstants.MSG_SUCCESS, goodsBannerList, 0, "", request);
    }


    /**
     * 增加banner
     *
     * @param request
     * @return
     */
    @Transactional
    @ResponseBody
    @RequestMapping("/addBanner")
    public Response addBanner(HttpServletRequest request) {
        String Id = request.getParameter("Id");
        String specId = request.getParameter("specId");
        String bannerUrl = request.getParameter("bannerUrl");
        String bannerImgUrl = request.getParameter("bannerImgUrl");
        String bannerState = request.getParameter("bannerState");
        String sort = request.getParameter("sort");
        int goodsId = 0;
        int specIds = 0;
        int sorts = 0;

        if (Id != null || Id != "" || !StringUtil.isNumeric(Id)) {
            goodsId = Integer.parseInt(Id);
        }
        if (specId != null || specId != "" || !StringUtil.isNumeric(specId)) {
            specIds = Integer.parseInt(specId);
        }
        if (sort != null || sort != "" || !StringUtil.isNumeric(sort)) {
            sorts = Integer.parseInt(sort);
        }

        GoodsBanner goodsBanner = new GoodsBanner();
        goodsBanner.setBannerImgUrl(bannerImgUrl);
        goodsBanner.setBannerState(bannerState);
        goodsBanner.setBannerUrl(bannerUrl);
        goodsBanner.setSort(sorts);

        int row = goodsBannerService.insertSelective(goodsBanner);
        if (row > 0) {
            //清除缓存
            redisTemplate.delete("banner");
            return new Response(MessageConstants.MSG_SUCCESS, goodsBanner, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, goodsBanner, 0, "", request);
        }
    }

    /**
     * 删除banner
     *
     * @return
     */
    @Transactional
    @ResponseBody
    @RequestMapping("/delBanner")
    public Response delBanner(HttpServletRequest request) {
        String bannerId = request.getParameter("bannerId");
        int bannerIds = 0;
        if (bannerId == null && bannerId == "" && !StringUtil.isNumeric(bannerId)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        bannerIds = Integer.parseInt(bannerId);
        int row = goodsBannerService.deleteByPrimaryKey(bannerIds);
        if (row > 0) {
            //清除缓存
            redisTemplate.delete("banner");
            return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }

    }


    /**
     * 删除banner
     *
     * @return
     */
    @Transactional
    @ResponseBody
    @RequestMapping("/editBanner")
    public Response editBanner(HttpServletRequest request) {

        int bannerId = 0;
        String Id = request.getParameter("Id");
        String specId = request.getParameter("specId");
        String bannerUrl = request.getParameter("bannerUrl");
        String bannerImgUrl = request.getParameter("bannerImgUrl");
        String bannerState = request.getParameter("bannerState");
        String sort = request.getParameter("sort");
        String bannerIds = request.getParameter("bannerId");
        int goodsId = 0;
        int specIds = 0;
        int sorts = 0;

        if (bannerIds == null && bannerIds == "" || !StringUtil.isNumeric(bannerIds)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        bannerId = Integer.parseInt(bannerIds);

        if (sort != null && sort != "") {
            sorts = Integer.parseInt(sort);
        }

        if (Id != null && Id != "") {
            goodsId = Integer.parseInt(Id);
        }

        if (specId != null && specId != "") {
            specIds = Integer.parseInt(specId);
        }


        GoodsBanner goodsBanner = new GoodsBanner();
        goodsBanner.setSort(sorts);
        goodsBanner.setBannerUrl(bannerUrl);
        goodsBanner.setBannerImgUrl(bannerImgUrl);
        goodsBanner.setBannerState(bannerState);
        goodsBanner.setGoodsId(goodsId);
        goodsBanner.setSpecId(specIds);
        goodsBanner.setBannerId(bannerId);
        int row = goodsBannerService.updateByPrimaryKeySelective(goodsBanner);
        if (row > 0) {
            //清除缓存
            redisTemplate.delete("banner");
            return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }

    }


    @ResponseBody
    @RequestMapping("/shortURL")
    public void getShortURL(HttpServletRequest request, HttpServletResponse response) {
        logger.info("获取短链接：-------->" + request.getQueryString());

        String shortURLParam = null;
        String urlParam = null;
        shortURLParam = request.getParameter("shortUrl");
        if (!shortURLParam.isEmpty()) {
            Object obj = redisTemplate.opsForValue().get(shortURLParam);
            if (obj != null) {
                urlParam = (String) obj;


            } else {

                ShortUrl shortUrl = shortUrlService.selectByKey(shortURLParam);
                if (shortUrl != null) {
                    urlParam = shortUrl.getShortUrl();
                }

            }

            try {
                response.sendRedirect(HttpRequestConfig.webServerUrl + urlParam);
            } catch (IOException e) {
                e.printStackTrace();

            }
        } else {
            try {
                response.sendRedirect(HttpRequestConfig.webServerUrl);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }


}



